//
//  PanGestureViewController.swift
//  testRxSwift
//
//  Created by 火星人 on 2017/7/27.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit
import RxGesture
import RxCocoa
import RxSwift

class PanGestureViewController: BaseViewController {

    let disposed = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tapView = UIView(frame: CGRect(x: 0, y: 80, width: 80, height: 80))
        tapView.backgroundColor = UIColor.randomColor()
        self.view.addSubview(tapView)
        
        let panGesture = tapView.rx.panGesture().shareReplay(1)
        var x = tapView.x
        var y = tapView.y
        
        panGesture.when(.began)
            .asTranslation()
            .subscribe(onNext: { (translation, velocity) in
                x = tapView.transform.tx
                y = tapView.transform.ty
            })
            .disposed(by: disposed)
        
        panGesture.when(.changed)
            .asTranslation()
            .subscribe(onNext: { (translation, velocity) in
                tapView.transform = CGAffineTransform(translationX: translation.x + x, y: translation.y + y)
            })
            .disposed(by: disposed)
        
        panGesture.when(.ended)
            .subscribe { (eve) in
                
                print(tapView.frame)
            }
            .disposed(by: disposed)
    }


}
